package com.zrrd.yunchmall.user.controller;

import com.zrrd.yunchmall.pojo.UserTmp;
import com.zrrd.yunchmall.user.service.UserServiceImpl;
import com.zrrd.yunchmall.user.util.JwtUtil;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Liangyaoyun
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/user")
@Api(value = "用户服务接口",tags = "用户服务接口")
public class userController {
    @Autowired
    public UserServiceImpl userService;
    @GetMapping("/{uid}")
    @ApiOperation(value = "查询用户详情",httpMethod = "GET")
    public UserTmp selectByUid(@ApiParam(name = "用户Id",required = true) @PathVariable("uid") int uid){
        return userService.getById(uid);
    }


    @RequestMapping("/login")
    @ApiOperation(value = "登录",httpMethod = "POST")
    //
    public ResponseResult<String> login(@ApiParam(name = "username",value ="用户账号" ,required = true)
                                                   @RequestParam("username") String username,
                                        @ApiParam(name = "password",value = "密码",required = true)
                                              @RequestParam("password") String password){
        String token=userService.login(username,password);
        if (token!=null){
            return new ResponseResult<>(200,"登录成功",token);
        }else {
            return new ResponseResult<>(401,"用户名或密码错误",null);
        }
    }

    @RequestMapping("/login2")
    @ApiOperation(value = "登录2", httpMethod = "POST")
    //
    public ResponseResult<String> login2(@ApiParam(required = true)@RequestBody UserTmp userTmp) {
        String token = userService.login(userTmp.getUsername(), userTmp.getPassword());
        if (token != null) {
            return new ResponseResult<>(200, "登录成功", token);
        } else {
            return new ResponseResult<>(401, "用户名或密码错误", null);
        }
    }


    @RequestMapping("/auth")
    public UserTmp auth(String token){
        //去除token前面的多余字符和空格
        return JwtUtil.parse(token.substring(7));
    }


}
